<template>

  <div style="display: flex; flex-direction: column; align-items: center;">
  <el-table :data="tableData" >
    <el-table-column prop="textbookId" label="教材Id">
    </el-table-column>
    <el-table-column prop="demandNum" label="教材数目">
    </el-table-column>
    <el-table-column prop="departmentId" label="系Id">
    </el-table-column>
    <el-table-column >
      <template v-slot="scope">
        <el-button type="primary" @click="GetDetail(scope.row)">修改</el-button>
      </template>
    </el-table-column>
  </el-table>



    <el-pagination
        :page-size="pageSize"
        :pager-count="pageNum"
        :total="totalCount"
        layout="total, prev, pager, next, jumper"
        @current-change="handleCurrentChange"
        @size-change="handleSizeChange">
    </el-pagination>

    <el-form :inline="true">
      <el-form-item>
        <el-input v-model="changeTextbookId" @input="handleNumberInput('changeTextbookId')" placeholder="教材ID"></el-input>
      </el-form-item>
      <el-form-item>
        <el-input v-model="changeTextbookName" placeholder="教材名"></el-input>
      </el-form-item>
      <el-form-item>
        <el-input v-model="changeCourseName" placeholder="课程名"></el-input>
      </el-form-item>
      <el-form-item>
        <el-input v-model="changePublisherId" @input="handleNumberInput('changePublisherId')" placeholder="出版社ID"></el-input>
      </el-form-item>
      <el-form-item>
        <el-input v-model="changePrice" @input="handleNumberInput('changePrice')" placeholder="标价"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="InsertText">增加</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="DeleteText">删除</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="ChangeText">修改</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="AssessDepartText" style="background-color: green; border-color: green;">确认</el-button>
      </el-form-item>

    </el-form>

  </div>
</template>

<script>
import request from "@/utils/request";
import {Message} from "element-ui";
import userManger from "@/UserManager";

export default {
  data() {
    return {
      pageSize: 13,
      pageNum: 1,
      totalCount: 0,
      tableData: [],
      departmentId:userManger.getUserId(),
      changeTextbookId: '',
      changeTextbookName: '',
      changeCourseName: '',
      changePublisherId: '',
      changePrice: ''

    }
  },

  mounted() {
    this.GetDepartText()
  },
  methods: {
    GetDepartText() {
      const GetDepartTextRequest = {
        departmentId: this.departmentId,
        pageNum:this.pageNum,
        pageSize:this.pageSize,

      }
      request.post("/department/demand", GetDepartTextRequest)
          .then(res => {
            console.log(res.data);
            this.tableData = res.data.data.list;
            this.totalCount = res.data.data.totalCount;
            if(this.totalCount==0)
              this.showMsg('没有需要确认的记录');

          })
          .catch(error => {
            console.error("Error fetching data:", error);
          });
    },
    AssessDepartText() {
      // 发送 access 请求的逻辑
      const AssessDepartTextRequest = {
        ints:[this.departmentId],

      }
      request.post("/department/confirm",AssessDepartTextRequest)  // 假设发送 GET 请求到 /access 接口
          .then(res => {
            console.log("Access request successful:", res.data);
            // 在这里处理返回的数据或者更新组件状态
            this.Msg('确认成功');
            this.GetDepartText();
          })
          .catch(error => {
            console.error("Error sending access request:", error);
          });
    },
    showError(str) {
      // 触发错误提示
      Message.error({
        message: str,
        duration: 3000 // 持续时间为3000毫秒（3秒）
      });
    },
    showMsg(str) {
      // 触发错误提示
      Message.info({
        message: str,
        duration: 3000 // 持续时间为3000毫秒（3秒）
      });
    },
    handleNumberInput(field) {
      this.form[field] = this.form[field].replace(/\D/g, ''); // 只保留数字字符
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pageSize = val;
      this.pageNum = 1;
      this.GetDepartText();
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.pageNum = val;
      this.GetDepartText();
    },

    InsertText() {
      const InsertTextRequest = {
        departmentId: Number(this.departmentId),
        textbookId: Number(this.changeTextbookId),
        textbookName: this.changeTextbookName,
        courseName: this.changeCourseName,
        pressId: Number(this.changePublisherId),
        price: Number(this.changePrice)

      }
      request.post("/textbook/addTextbook", InsertTextRequest)
          .then(res => {
            console.log(res.data);
            if(res.data.isRight)
            {
              this.showMsg('修改成功');
            }
            else
              rhis.showError(res.data.error);
            this.GetDepartText();

          })
          .catch(error => {
            console.error("Error fetching data:", error);
          });
    },
    DeleteText() {
      const DeleteTextRequest = {
        departmentId: Number(this.departmentId),
        textbookId: Number(this.changeTextbookId),
        textbookName: this.changeTextbookName,
        courseName: this.changeCourseName,
        pressId: Number(this.changePublisherId),
        price: Number(this.changePrice)

      }
      request.post("/textbook/deleteTextbook", DeleteTextRequest)
          .then(res => {
            console.log(res.data);
            if(res.data.isRight)
            {
              this.showMsg('修改成功');
            }
            else
              rhis.showError(res.data.error);
            this.GetDepartText();

          })
          .catch(error => {
            console.error("Error fetching data:", error);
          });
    },
    ChangeText() {
      const ChangeTextRequest = {
        departmentId: Number(this.departmentId),
        textbookId: Number(this.changeTextbookId),
        textbookName: this.changeTextbookName,
        courseName: this.changeCourseName,
        pressId: Number(this.changePublisherId),
        price: Number(this.changePrice)

      }
      request.post("/textbook/updateTextbook", ChangeTextRequest)
          .then(res => {
            console.log(res.data);
            if(res.data.isRight)
            {
              this.showMsg('修改成功');
            }
            else
              rhis.showError(res.data.error);
            this.GetDepartText();
          })
          .catch(error => {
            console.error("Error fetching data:", error);
          });
    },
    GetDetail(val){
      console.log('val',val)
      this.departmentId=val.departmentId
          this.changeTextbookId= val.textbookId
      console.log(this.changeTextbookId)
          // this.changeTextbookName= val.changeTextbookName
          // this.changeCourseName= val.changeCourseName
          // this.changePublisherId= val.changePublisherId
          // this.changePrice= val.changePrice
    },

  }
};
</script>
